package com.ruoyi.project.system.goodsclass.service.impl;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uniquekey.IdGererateFactory;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.framework.web.domain.ZtreeStr;
import com.ruoyi.project.system.goodsclass.dto.GoodsClassDto;
import com.ruoyi.project.system.goodsclass.mapper.GoodsClassMapper;
import com.ruoyi.project.system.goodsclass.domain.GoodsClass;
import com.ruoyi.project.system.goodsclass.service.IGoodsClassService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 商品类型Service业务层处理
 * 
 * @author LCL
 * @date 2020-06-06
 */
@Service
public class GoodsClassServiceImpl implements IGoodsClassService 
{
    @Resource
    private IdGererateFactory idGererateFactory;
    @Autowired
    private GoodsClassMapper goodsClassMapper;

    /**
     * 查询商品类型
     * 
     * @param id 商品类型ID
     * @return 商品类型
     */
    @Override
    public GoodsClass selectGoodsClassById(String id)
    {
        return goodsClassMapper.selectGoodsClassById(id);
    }

    /**
     * 查询商品类型列表
     * 
     * @param goodsClass 商品类型
     * @return 商品类型
     */
    @Override
    public List<GoodsClass> selectGoodsClassList(GoodsClass goodsClass)
    {
        return goodsClassMapper.selectGoodsClassList(goodsClass);
    }

    /**
     * 新增商品类型
     * 
     * @param goodsClass 商品类型
     * @return 结果
     */
    @Override
    public int insertGoodsClass(GoodsClass goodsClass)
    {
        goodsClass.setId(this.idGererateFactory.nextStringId());
        return goodsClassMapper.insertGoodsClass(goodsClass);
    }

    /**
     * 修改商品类型
     * 
     * @param goodsClass 商品类型
     * @return 结果
     */
    @Override
    public int updateGoodsClass(GoodsClass goodsClass)
    {
        return goodsClassMapper.updateGoodsClass(goodsClass);
    }

    /**
     * 删除商品类型对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGoodsClassByIds(String ids)
    {
        return goodsClassMapper.deleteGoodsClassByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商品类型信息
     * 
     * @param id 商品类型ID
     * @return 结果
     */
    @Override
    public int deleteGoodsClassById(String id)
    {
        return goodsClassMapper.deleteGoodsClassById(id);
    }

    /**
     * 加载商品分类菜单列表树
     */
    @Override
    public List<ZtreeStr> goodsclassTreeData(String all) {
        List<GoodsClass> goodsClasses = this.goodsClassMapper.findAll(all);
        List<ZtreeStr> ztrees = initZtreeStr(goodsClasses);
        return ztrees;
    }

    @Override
    public List<GoodsClass> findAll(String all) {
        return this.goodsClassMapper.findAll(all);
    }

    @Override
    public List<GoodsClass> selectGoodsClassByIds(String ids) {
        return this.goodsClassMapper.selectGoodsClassByIds(Convert.toStrArray(ids));
    }

    @Override
    public GoodsClassDto selectGoodsDtoClassById(String id) {
        return this.goodsClassMapper.selectGoodsDtoClassById(id);
    }

    @Override
    public List<GoodsClass> findGoodsClassByParentIds(String ids) {
        return this.goodsClassMapper.findGoodsClassByParentIds(Convert.toStrArray(ids));
    }

    @Override
    public List<GoodsClass> findApiList() {
        return this.goodsClassMapper.findApiList();
    }


    /**
     * 对象转菜单树
     *
     * @param goodsClasses 菜单列表
     * @return 树结构列表
     */
    public List<ZtreeStr> initZtreeStr(List<GoodsClass> goodsClasses)
    {
        return initZtreeStr(goodsClasses, null, false);
    }

    /**
     * 对象转菜单树
     *
     * @param goodsClasses 菜单列表
     * @param roleMenuList 角色已存在菜单列表
     * @param permsFlag 是否需要显示权限标识
     * @return 树结构列表
     */
    public List<ZtreeStr> initZtreeStr(List<GoodsClass> goodsClasses, List<String> roleMenuList, boolean permsFlag)
    {
        List<ZtreeStr> ztrees = new ArrayList<ZtreeStr>();
        boolean isCheck = StringUtils.isNotNull(roleMenuList);
        for (GoodsClass goodsClass : goodsClasses)
        {
            ZtreeStr ztree = new ZtreeStr();
            ztree.setId(goodsClass.getId());
            ztree.setpId(goodsClass.getParentId());
            ztree.setName(transGoodsclassName(goodsClass, permsFlag));
            ztree.setTitle(goodsClass.getName());
            if (isCheck)
            {
//                ztree.setChecked(roleMenuList.contains(goodsClass.getId() + menu.getPerms()));
                ztree.setChecked(false);
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }

    public String transGoodsclassName(GoodsClass goodsClass, boolean permsFlag)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(goodsClass.getName());
        if (permsFlag)
        {
//            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + goodsClass.getPerms() + "</font>");
        }
        return sb.toString();
    }

}
